const MarketEnvironment = require('./core/MarketEnvironment');
const Asset = require('./core/Asset');
const MacroEvent = require('./core/MacroEvent');
const RetailInvestor = require('./investors/RetailInvestor');
const MarketMaker = require('./investors/MarketMaker');
const InstitutionalInvestor = require('./investors/InstitutionalInvestor');
const RandomStrategy = require('./strategies/RandomStrategy');
const MarketVisualizer = require('./visualization/MarketVisualizer');

// 创建市场环境
const market = new MarketEnvironment({
  timeStep: 1,
  macroEventProbability: 0.1, // 10%概率触发宏观事件
  investorGeneratorConfig: {
    entryRate: 0.2, // 20%概率生成新投资者
    investorTypes: [
      { type: 'RetailInvestor', weight: 0.7 },
      { type: 'MarketMaker', weight: 0.2 },
      { type: 'InstitutionalInvestor', weight: 0.1 }
    ]
  }
});

// 添加资产
const stockA = new Asset('AAPL', 'Apple Inc.', 150.00, { volatility: 0.02 });
const stockB = new Asset('GOOG', 'Alphabet Inc.', 2500.00, { volatility: 0.015 });

market.addAsset(stockA);
market.addAsset(stockB);

// 添加宏观事件
const events = [
  new MacroEvent({ name: '经济数据好于预期', type: 'economic', impact: 2, duration: 3, probability: 0.1 }),
  new MacroEvent({ name: '地缘政治紧张', type: 'political', impact: -3, duration: 5, probability: 0.05 }),
  new MacroEvent({ name: '央行降息', type: 'economic', impact: 4, duration: 2, probability: 0.05 }),
  new MacroEvent({ name: '重大技术突破', type: 'technology', impact: 5, duration: 4, probability: 0.03 })
];

events.forEach(event => market.addMacroEvent(event));

// 添加初始投资者
const retail1 = new RetailInvestor({ 
  name: '散户张三', 
  capital: 50000,
  strategy: new RandomStrategy()
});

const retail2 = new RetailInvestor({ 
  name: '散户李四', 
  capital: 30000,
  strategy: new RandomStrategy()
});

const marketMaker = new MarketMaker({ 
  name: '做市商A', 
  capital: 1000000,
  strategy: new RandomStrategy()
});

const institutional = new InstitutionalInvestor({ 
  name: '机构投资者', 
  capital: 5000000,
  strategy: new RandomStrategy()
});

market.addInvestor(retail1);
market.addInvestor(retail2);
market.addInvestor(marketMaker);
market.addInvestor(institutional);

// 运行模拟
console.log('开始金融市场模拟');

for (let i = 0; i < 30; i++) {
  console.log(`\n========== 第 ${i + 1} 步 ==========`);
  market.advanceTime();
  
  // 显示市场概览
  MarketVisualizer.displayMarketOverview(market);
  
  // 显示活跃的宏观事件
  if (market.activeEvents.length > 0) {
    console.log('\n--- 活跃宏观事件 ---');
    market.activeEvents.forEach(event => {
      console.log(`${event.name} (剩余时间: ${event.remainingDuration})`);
    });
  }
  
  // 显示部分投资者持仓
  if (i % 10 === 9) { // 每10步显示一次详细信息
    const prices = market.getAllAssetPrices();
    MarketVisualizer.displayInvestorPositions(retail1, prices);
    MarketVisualizer.displayInvestorPositions(institutional, prices);
  }
}

console.log('\n========== 模拟结束 ==========');
MarketVisualizer.displayMarketOverview(market);